package com.ts.api.module.match.plan.internal.distribute.model;

import com.ts.api.common.constant.productflow.ProductApplyFlowStatusEnum;
import com.ts.api.module.api.convert.core.http.client.ApiHttpMessage;
import com.ts.api.module.product.entity.po.ProductApplyFlowPO;
import com.ts.api.module.product.entity.po.ProductPO;
import com.ts.api.module.product.entity.po.ProductPlanPO;
import lombok.Data;

/**
 *  计划分发结果对象
 *
 *  @author Pan
 */
@Data
public class PlanDistributeResult {

    /** 本次请求id */
    private String reqId;
    /** 主体信息 */
    private ProductPO productPO;
    /** 主体计划信息 */
    private ProductPlanPO productPlanPO;
    /** 返回匹配字段结果 */
    private PlanDistributeField planDistributeField;
    /** 当前分发流程结果 */
    private ProductApplyFlowPO currentDistributeFlow;
    /** http响应后的结果 */
    private ApiHttpMessage apiHttpMessage;
    /** 分发结果状态 */
    private Boolean distributeStatus = false;
    /** 业务错误信息 */
    private String bizErrorMsg;
    /** 业务错误返回状态 */
    private ProductApplyFlowStatusEnum bizErrorType;

    /**
     *  修改分发流程为成功状态
     */
    public PlanDistributeResult modifyDistributeFlowSuc() {
        this.currentDistributeFlow.setBizStatus("1");
        return this;
    }

    /**
     *  构造
     *
     *  @param  productPO               主体
     *  @param  productPlanPO           计划
     *  @param  currentDistributeFlow   当前流程
     *  @return PlanDistributeResult
     */
    private static PlanDistributeResult of(ProductPO productPO, ProductPlanPO productPlanPO, ProductApplyFlowPO currentDistributeFlow) {
        PlanDistributeResult planDistributeResult = new PlanDistributeResult();
        planDistributeResult.setProductPO(productPO);
        planDistributeResult.setProductPlanPO(productPlanPO);
        planDistributeResult.setCurrentDistributeFlow(currentDistributeFlow);
        return planDistributeResult;
    }

    /**
     *  构建失败分发
     *
     *  @return PlanDistributeResult    分发结果对象
     */
    public static PlanDistributeResult ofFail() {
        return new PlanDistributeResult();
    }

    /**
     *  构建失败分发
     *
     *  @return PlanDistributeResult    分发结果对象
     */
    public static PlanDistributeResult ofFail(String bizErrorMsg) {
        PlanDistributeResult planDistributeResult = new PlanDistributeResult();
        planDistributeResult.setBizErrorMsg(bizErrorMsg);
        return planDistributeResult;
    }

    /**
     *  构建失败分发
     *
     *  @param  planDistribute          分发对象
     *  @return PlanDistributeResult    分发结果对象
     */
    public static PlanDistributeResult ofFail(PlanDistribute planDistribute) {
        return of(planDistribute.getProductPO(), planDistribute.getProductPlanPO(), planDistribute.getCurrentDistributeFlow());
    }

    /**
     *  构建失败分发
     *
     *  @param  planDistribute          分发对象
     *  @return PlanDistributeResult    分发结果对象
     */
    public static PlanDistributeResult ofFail(PlanDistribute planDistribute, String bizErrorMsg) {
        PlanDistributeResult planDistributeResult = of(planDistribute.getProductPO(), planDistribute.getProductPlanPO(), planDistribute.getCurrentDistributeFlow());
        planDistributeResult.setBizErrorMsg(bizErrorMsg);
        return planDistributeResult;
    }

    /**
     *  构建失败分发
     *
     *  @param  planDistributePost      分发后对象
     *  @return PlanDistributeResult    分发结果对象
     */
    public static PlanDistributeResult ofFail(PlanDistributePost planDistributePost) {
        PlanDistribute planDistribute = planDistributePost.getPlanDistribute();
        ProductPO productPO = planDistribute.getProductPO();
        ProductPlanPO productPlanPO = planDistribute.getProductPlanPO();
        ProductApplyFlowPO currentDistributeFlow = planDistribute.getCurrentDistributeFlow();

        return of(productPO, productPlanPO, currentDistributeFlow);
    }

    /**
     *  构建成功分发
     *
     *  @param  planDistributePost          分发后对象
     *  @return PlanDistributeResult
     */
    public static PlanDistributeResult ofSuc(PlanDistributePost planDistributePost) {
        ProductPO productPO = planDistributePost.getPlanDistribute().getProductPO();
        ProductPlanPO productPlanPO = planDistributePost.getPlanDistribute().getProductPlanPO();
        ProductApplyFlowPO currentDistributeFlow = planDistributePost.getPlanDistribute().getCurrentDistributeFlow();

        PlanDistributeResult planDistributeResult = of(productPO, productPlanPO, currentDistributeFlow);
        planDistributeResult.setDistributeStatus(true);
        planDistributeResult.setPlanDistributeField(planDistributePost.getPlanDistributeField());
        return planDistributeResult;
    }

    /**
     *  构建成功流程
     *
     *  @return PlanDistributeResult
     */
    public static PlanDistributeResult ofEmptySuc() {
        PlanDistributeResult planDistributeResult = new PlanDistributeResult();
        planDistributeResult.setDistributeStatus(true);
        return planDistributeResult;
    }

    /**
     *  构建成功流程
     *
     *  @param  planDistribute          分发对象
     *  @return PlanDistributeResult
     */
    public static PlanDistributeResult ofEmptySuc(PlanDistribute planDistribute) {
        PlanDistributeResult planDistributeResult = of(planDistribute.getProductPO(), planDistribute.getProductPlanPO(), planDistribute.getCurrentDistributeFlow());
        planDistributeResult.setDistributeStatus(true);
        return planDistributeResult;
    }

    /**
     *  构建成功流程
     *
     *  @param  planDistribute          分发对象
     *  @param  apiHttpMessage          API客户端实体
     *  @return PlanDistributeResult
     */
    public static PlanDistributeResult ofEmptySuc(PlanDistribute planDistribute, ApiHttpMessage apiHttpMessage) {
        PlanDistributeResult planDistributeResult = of(planDistribute.getProductPO(), planDistribute.getProductPlanPO(), planDistribute.getCurrentDistributeFlow());
        planDistributeResult.setApiHttpMessage(apiHttpMessage);
        planDistributeResult.setDistributeStatus(true);
        return planDistributeResult;
    }
}

